<?php
namespace Admin\Controller;
use Admin\Common\BaseController;
header('content-type:text/html;charset=utf-8;');
class CarouselController extends BaseController {
	public function index()
	{
		$carousel = M('carousel');
		
		//查询接收的条数
		$count = $carousel -> count();
		//实例化分页类
		$page = new \Think\Page($count,10);//实例化分页类 传入总记录数和显示记录数
		$show = $page -> show();//分页显示输出
		$limit = $page -> firstRow.','.$page -> listRows;
		$data = $carousel -> limit($limit) -> order('sort asc') -> select();
		// var_dump($data);
		//分配变量
    	$this -> pagestr = $show;
		$this -> assign("show",$show);
		$this -> assign("data",$data);
		$this -> assign('title','轮播列表');
		//解析模板
		$this -> display();
	}

	public function add()
	{
		//解析模板
		$this -> display();
	}

	public function insert()
	{
		//实例化
		$carousel = M("carousel");
		
		if(!empty($_FILES['pic']['name'])){	
			//判断是否上传头像
			if($_FILES['pic']['error'] != 0)
			{
				// $this -> error("上传头像有误");
				echo "<script>
					alert('上传头像有误');
					window.location.href='".U('Admin/Carousel/add')."';
				</script>";
				die();
			}
			//处理图片
			$upload = new \Think\Upload();//实例化上传类
			$upload -> maxSize = 3145728;//设置附件上传大小
			$upload -> exts = array('jpg','png','gif','jpeg');//设置附件上传类型
			$upload ->rootPath = './Public/';// 设置附件上传根目录  
			$upload ->savePath = '/Uploads/Carousel/';// 设置附件上传子目录  

			$info = $upload -> upload();
			
			if(!$info) {// 上传错误提示错误信息
		        echo "<script>
						alert('".$upload->getError()."');
						window.location.href='".U('Admin/Carousel/add')."';
				</script>";
				die();
				// $this->error($upload->getError());
		    }else{// 上传成功
		    	$_POST["pic"] = $info["pic"]["savepath"].$info["pic"]["savename"];
		        echo "<script>
					alert('添加成功');
					window.location.href='".U('Admin/Carousel/index')."';
					</script>";
		    }
		}
		//过滤数据
		$carousel -> create();
		//添加
		$carousel -> add();

	}

	//修改
	public function edit()
	{
		//接收ID
		$id = I('get.id');
		//实例化上传类
		$carousel = M('carousel');
		//查询
		$data = $carousel -> where("id = '".$id."'") -> find();
		//分配变量
		$this -> assign('title','轮播更改');
		$this -> assign('carousel','轮播更改');
		$this -> assign('data',$data);
		$this -> display();
	}
	//执行更改
	public function update()
	{
		
		//实例化
		$carousel = M('carousel');
		//开启事务
		$carousel -> startTrans();
		$id = (int)I('post.id');

		if(!empty($_FILES['pic']['name'])){
			//判断是否上传头像
			if($_FILES['pic']['error'] != 0)
			{
				// $this -> error("上传头像有误");
				echo "<script>
					alert('上传头像有误');
					window.location.href='".U('Admin/Carousel/index')."';
				</script>";
				die();
			}

			//处理图片
			//实例化上传类
			$upload = new \Think\Upload();
			$upload -> maxSize = 3145728;//设置附件上传大小
			$upload -> exts = array('jpg','jpeg','png','gif');//设置上传类型
			$upload -> rootPath = './Public/';//设置附件上传根目录
			$upload -> savePath = '/Uploads/Carousel/';//设置附件上传子目录
			//上传文件
			$info = $upload -> upload();

			if(!$info)
			{
				//上传错误提示错误信息
				//$this -> error($upload -> getError());
				echo "<script>
					alert('".$upload -> getError()."');
					window.location.href='".U('Admin/Carousel/index')."';
				</script>";
				die();
			}else{
				$_POST["pic"] = $info["pic"]["savepath"].$info["pic"]["savename"];
			}
			//删除头像
			$row = $carousel -> where("id = '".$_POST['id']."'") -> find();
			$img = rtrim($_SERVER["SCRIPT_FILENAME"],"index.php")."Public".$row["pic"];
			//过滤数据
			$carousel -> create();
			//修改表
			$ree = $carousel -> save();
			if($ree)
			{
				//提交事务
				$carousel -> commit();
				unlink($img);
				echo "<script>
						alert('修改成功');
						window.location.href='".U('Admin/Carousel/index')."';
				</script>";
			}else{
				//回滚事务
				$carousel -> rollback();
				echo "<script>
						alert('修改失败');
						window.location.href='".U('Admin/Carousel/index',array('id'=>$id))."';
				</script>";
			}
		}else{
			//删除图片
			$row = $carousel -> where("id = '".$id."'") -> find();
			$img = rtrim($_SERVER["SCRIPT_FILENAME"],"index.php")."Public".$row["pic"];
			//修改表
			$carousel -> create();
			$ree = $carousel -> save();

			if($ree)
			{
				//提交事务
				$carousel -> commit();
				unlink($img);
				echo "<script>
						alert('修改成功');
						window.location.href='".U('Admin/Carousel/index')."';
				</script>";
			}else{
				//回滚事务
				$carousel -> rollback();
				echo "<script>
						alert('修改失败');
						window.location.href='".U('Admin/Carousel/index',array('id'=>$id))."';
				</script>";
			}
		}
		

	}

	public function delete(){
		$id = I('post.id');
		$carousel = M('carousel');
		//删除图片
		$row = $carousel -> where("id = '".$id."'") -> find();
		$img = rtrim($_SERVER["SCRIPT_FILENAME"],"index.php")."Public".$row["pic"];

		$dele = $carousel -> where('id = "'.$id.'"') -> delete();
		if($dele)
        {
            echo 1;
            unlink($img);
        }else{
            echo 2;
        }
	}
	//批量删除
	public function del()
	{
		//接收ID
		$id = I('post.id');
		
		//判断没选中的情况
		if(empty($id))
		{
			echo "<script>
				alert('删除失败');
				window.location.href='".U('Admin/Carousel/index')."';
			</script>";
		}

		//转为字符串
		$sid = implode(',',$id);
		
		//in 方法
		$where['id'] = array('in',$sid);

		$wh['uid'] = array('in',$sid);

		$whe['jid'] = array('in',$sid);
		
		//实例化
		$carousel = M('carousel');
		// 删除头像
		// $row = $carousel -> where("id",$sid) -> select();
		$row = $carousel -> where($where) -> select();
		// $img = rtrim($_SERVER["SCRIPT_FILENAME"],"index.php")."Public".$row["pic"];
		foreach($row as $k => $v){
			// echo $v;
			$img = rtrim($_SERVER["SCRIPT_FILENAME"],"index.php")."Public".$v["pic"];
			unlink($img);
		}
		
		//删除
		$dd = $carousel -> where($where) -> delete();
		//判断
		if($dd)
		{
            echo "<script>
				alert('删除成功');
				window.location.href='".U('Admin/Carousel/index')."';
			</script>";
            
		}else{
			echo "<script>
				alert('删除失败');
				window.location.href='".U('Admin/Carousel/index')."';
			</script>";
		}
	}
}